package com.qms.qep.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qms.common.utils.DateUtils;
import com.qms.qep.domain.ProcessRecord;
import com.qms.qep.domain.vo.ProductionStatistics;
import com.qms.qep.mapper.ProcessRecordMapper;
import com.qms.qep.service.IProcessRecordService;

/**
 * 工序流转记录Service业务层处理
 * 
 * @author qep
 */
@Service
public class ProcessRecordServiceImpl implements IProcessRecordService 
{
    @Autowired
    private ProcessRecordMapper processRecordMapper;

    /**
     * 查询工序流转记录
     * 
     * @param id 工序流转记录主键
     * @return 工序流转记录
     */
    @Override
    public ProcessRecord selectProcessRecordById(Long id)
    {
        return processRecordMapper.selectProcessRecordById(id);
    }

    /**
     * 查询工序流转记录列表
     * 
     * @param processRecord 工序流转记录
     * @return 工序流转记录
     */
    @Override
    public List<ProcessRecord> selectProcessRecordList(ProcessRecord processRecord)
    {
        return processRecordMapper.selectProcessRecordList(processRecord);
    }
    
    /**
     * 根据批次ID查询工序流转记录列表
     * 
     * @param batchId 批次ID
     * @return 工序流转记录集合
     */
    @Override
    public List<ProcessRecord> selectProcessRecordByBatchId(Long batchId)
    {
        return processRecordMapper.selectProcessRecordByBatchId(batchId);
    }
    
    /**
     * 查询工序流转效率统计
     * 
     * @param params 查询参数
     * @return 工序效率统计数据
     */
    @Override
    public List<Map<String, Object>> selectProcessEfficiency(Map<String, Object> params)
    {
        return processRecordMapper.selectProcessEfficiency(params);
    }
    
    /**
     * 查询瓶颈工序分析
     * 
     * @param params 查询参数
     * @return 瓶颈工序分析数据
     */
    @Override
    public List<Map<String, Object>> selectBottleneckAnalysis(Map<String, Object> params)
    {
        return processRecordMapper.selectBottleneckAnalysis(params);
    }
    
    /**
     * 查询生产周期分析
     * 
     * @param params 查询参数
     * @return 生产周期分析数据
     */
    @Override
    public List<Map<String, Object>> selectProductionCycleAnalysis(Map<String, Object> params)
    {
        return processRecordMapper.selectProductionCycleAnalysis(params);
    }
    
    /**
     * 查询生产进度
     * 
     * @param params 查询参数
     * @return 生产进度数据
     */
    @Override
    public Map<String, Object> selectProductionProgress(Map<String, Object> params)
    {
        return processRecordMapper.selectProductionProgress(params);
    }
    
    /**
     * 获取生产统计数据
     * 
     * @param params 查询参数
     * @return 生产统计数据
     */
    @Override
    public ProductionStatistics getProductionStatistics(Map<String, Object> params)
    {
        ProductionStatistics statistics = new ProductionStatistics();
        
        // 获取工序效率数据
        statistics.setProcessEfficiencyData(processRecordMapper.selectProcessEfficiency(params));
        
        // 获取瓶颈工序分析
        statistics.setBottleneckData(processRecordMapper.selectBottleneckAnalysis(params));
        
        // 获取生产周期分析
        statistics.setCycleTimeData(processRecordMapper.selectProductionCycleAnalysis(params));
        
        // 获取生产进度
        statistics.setProgressData(processRecordMapper.selectProductionProgress(params));
        
        // 设置产能利用率（模拟数据）
        statistics.setCapacityUtilization(85.5);
        
        // 延期风险预警（模拟数据）
        // TODO: 实现真实的延期风险预警逻辑
        List<Map<String, Object>> delayRiskWarnings = new ArrayList<>();
        
        Map<String, Object> warning1 = new HashMap<>();
        warning1.put("batchNo", "B2024030001");
        warning1.put("productName", "低中压锅炉管");
        warning1.put("riskLevel", "高");
        warning1.put("delayReason", "物料短缺");
        warning1.put("expectedDelay", "2天");
        delayRiskWarnings.add(warning1);
        
        Map<String, Object> warning2 = new HashMap<>();
        warning2.put("batchNo", "B2024030003");
        warning2.put("productName", "对焊弯头");
        warning2.put("riskLevel", "中");
        warning2.put("delayReason", "设备故障");
        warning2.put("expectedDelay", "1天");
        delayRiskWarnings.add(warning2);
        
        statistics.setDelayRiskWarnings(delayRiskWarnings);
        
        return statistics;
    }

    /**
     * 新增工序流转记录
     * 
     * @param processRecord 工序流转记录
     * @return 结果
     */
    @Override
    public int insertProcessRecord(ProcessRecord processRecord)
    {
        processRecord.setCreateTime(DateUtils.getNowDate());
        return processRecordMapper.insertProcessRecord(processRecord);
    }

    /**
     * 修改工序流转记录
     * 
     * @param processRecord 工序流转记录
     * @return 结果
     */
    @Override
    public int updateProcessRecord(ProcessRecord processRecord)
    {
        return processRecordMapper.updateProcessRecord(processRecord);
    }

    /**
     * 批量删除工序流转记录
     * 
     * @param ids 需要删除的工序流转记录主键
     * @return 结果
     */
    @Override
    public int deleteProcessRecordByIds(Long[] ids)
    {
        return processRecordMapper.deleteProcessRecordByIds(ids);
    }

    /**
     * 删除工序流转记录信息
     * 
     * @param id 工序流转记录主键
     * @return 结果
     */
    @Override
    public int deleteProcessRecordById(Long id)
    {
        return processRecordMapper.deleteProcessRecordById(id);
    }
} 